package com.xing.morethread.config;

import com.xing.morethread.job.SyncUserJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Quartz定时任务配置类
 */

@Configuration
public class QuartsConfig {

    private static String JOB_GROUP_NAME = "PJB_JOBGROUP_NAME";
    private static String TRIGGER_GROUP_NAME = "PJB_TRIGGERGROUP_NAME";

    /**
     * 定时任务1：
     * 同步用户信息job任务详情
     *
     * @return
     */
    @Bean
    public JobDetail syncUserJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(SyncUserJob.class)
                .withIdentity("syncUserJobDetail", JOB_GROUP_NAME)
                .usingJobData("userName", "xfh的博客")
                .storeDurably()
                .build();
        return jobDetail;
    }

    /**
     * 定时任务1
     * 同步用户信息job触发器
     */
    @Bean
    public Trigger syncUserJobTrigger() {

        //每隔5秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");

        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(syncUserJobDetail()) //关联上面的JobDetail
                .withIdentity("syncUserJobTrigger", TRIGGER_GROUP_NAME)
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }

}
